package com.hs.hlw.credit.system.web.controller.api;

import com.hs.hlw.credit.system.business.dto.ResponseDto;
import com.hs.hlw.credit.system.business.dto.SubmissionFormDto;
import com.hs.hlw.credit.system.business.dto.UserDto;
import com.hs.hlw.credit.system.business.service.SubmissionFormService;
import com.hs.hlw.credit.system.common.util.BeanUtil;
import com.hs.hlw.credit.system.data.DataUtils;
import com.hs.hlw.credit.system.data.mybatis.Pageable;
import com.hs.hlw.credit.system.data.primary.model.SubmissionForm;
import com.hs.hlw.credit.system.web.util.RequestUtil;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.ArrayList;
import java.util.List;

/**
 * SubmissionFormController
 * @author zhb
 * @date 2018/11/19
 */
@RestController
@RequestMapping("/api/submission/form")
public class SubmissionFormController {
    private @Autowired SubmissionFormService submissionFormService;

    @PostMapping(path = "add")
    @ApiOperation(value = "新增表单配置", notes = "新增表单配置")
    public ResponseDto add(@RequestBody @Valid SubmissionFormDto.Add data, HttpServletRequest request) {
        UserDto user = RequestUtil.getUserFromSession(request);
        SubmissionForm submissionForm = BeanUtil.chang(data, SubmissionForm.class);
        submissionForm.setCreatorId(user.getId());
        submissionForm.setCreatorName(user.getUserName());
        return submissionFormService.add(submissionForm);
    }

    @PostMapping(path = "update")
    @ApiOperation(value = "更新表单配置", notes = "更新表单配置")
    public ResponseDto update(@RequestBody @Valid SubmissionFormDto.Update data, HttpServletRequest request) {
        UserDto user = RequestUtil.getUserFromSession(request);
        SubmissionForm submissionForm = BeanUtil.chang(data, SubmissionForm.class);
        submissionForm.setModifierId(user.getId());
        submissionForm.setModifierName(user.getUserName());
        return submissionFormService.update(submissionForm);
    }

    @GetMapping(path = "page")
    @ApiOperation(value = "分页查询报送表单", notes = "分页查询报送表单")
    public ResponseDto page(@RequestParam(required = false) @ApiParam(value = "表单名称") String name,
                            @RequestParam(required = false) @ApiParam(value = "状态(0: 禁用, 1: 上线, 2: 下线)") Short status,
                            @RequestParam(required = false, defaultValue = "1") @ApiParam(value = "查询页码", defaultValue = "1") Integer page,
                            @RequestParam(required = false, defaultValue = "10") @ApiParam(value = "每页数据条数", defaultValue = "10") Integer size,
                            @RequestParam(required = false, defaultValue = "id") @ApiParam(value = "排序字段（多个用逗号隔开）", defaultValue = "id") String orderField,
                            @RequestParam(required = false, defaultValue = "asc") @ApiParam(value = "排序规则（asc:升序，desc:降序，多个用逗号隔开）", defaultValue = "asc") String direction) {

        Pageable pageable = DataUtils.pageable(page, size, orderField, direction);
        pageable.addParam("name", name);
        pageable.addParam("status", status);
        return submissionFormService.page(pageable);
    }

    @GetMapping(path = "detail")
    @ApiOperation(value = "获取表单详情", notes = "获取表单详情")
    public ResponseDto detail(@RequestParam @ApiParam(value = "id", required = true) Integer id) {
        return submissionFormService.detail(id);
    }


    @PostMapping(path = "batchUpdateStatus")
    @ApiOperation(value = "批量更新表单配置", notes = "批量更新表单配置")
    public ResponseDto batchUpdateStatus(@RequestBody @Valid SubmissionFormDto.BatchUpdateStatus data, HttpServletRequest request) {
        List<SubmissionForm> list = new ArrayList<>();
        UserDto user = RequestUtil.getUserFromSession(request);
        for (SubmissionFormDto.SubmissionForm dto : data.getFormList()) {
            SubmissionForm submissionForm = new SubmissionForm();
            submissionForm.setId(dto.getId());
            submissionForm.setStatus(data.getStatus());
            submissionForm.setModifierId(user.getId());
            submissionForm.setModifierName(user.getUserName());
            list.add(submissionForm);
        }
        return submissionFormService.batchUpdateStatus(list);
    }
}